package com.waves.spring.aop.jdk;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

public class $Proxy0 implements JdkService {

    private final InvocationHandler h;

    public $Proxy0(InvocationHandler h) {
        this.h = h;
    }

    @Override
    public void eat() {
        try {
            h.invoke(this,eat, new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    @Override
    public int drink() {
        try {
            return (int) h.invoke(this,drink, new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    static Method eat;
    static Method drink;

    static {
        try {
            eat = JdkService.class.getMethod("eat");
            drink = JdkService.class.getMethod("drink");
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        }
    }

    public static void main(String[] args) {
        $Proxy0 proxy = new $Proxy0(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
                // 1. 前置增强
                System.out.println("前置通知增强...");
                // 2. 调用目标
                Object invoke = method.invoke(new Target(), params);
                // 3. 后置增强
                System.out.println("后置通知增强...");
                return invoke;
            }
        });
        proxy.eat();
        // 调用另一个方法
        System.out.println(proxy.drink());
    }
}
